﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication
{
    public partial class CreateNewShop
    {
        partial void CreateNewShop_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService> saveChangesTo)
        {
            // Write your code here.
            this.ShopProperty = new Shop();
            if (this.CustomerId.HasValue)
            {
                this.ShopProperty.ShopCustomer = this.DataWorkspace.ApplicationData.Customers.Where(e => e.Id == this.CustomerId).FirstOrDefault();
                this.FindControl("ShopCustomer").IsReadOnly = (this.ShopProperty.ShopCustomer != null);
            }
            if (this.SalesPersonId.HasValue)
            {
                this.ShopProperty.ShopSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(e => e.Id == this.SalesPersonId).FirstOrDefault();
                this.FindControl("ShopSalesPerson").IsReadOnly = (this.ShopProperty.ShopSalesPerson != null);
            }

            if (this.CustomerId.HasValue || this.SalesPersonId.HasValue) return; //no other check needed

            if (this.Application.User.HasPermission(Permissions.CanEditCustomer))//admin
            {
            }
            else if (this.Application.User.HasPermission(Permissions.CanManageMyShop))//sales person or customer
            {
                //for some reason LINQ does not work with external datasource link
                //var currentSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(s => s.aspnet_Profile.PropertyValuesString == userName).FirstOrDefault();
                //if (currentSalesPerson != null)
                //{
                //    this.SalesPersonId = currentSalesPerson.Id;
                //    this.VisitProperty.SalesPerson = currentSalesPerson;
                //    this.FindControl("SalesPerson").IsReadOnly = true;
                //}

                string userName = this.Application.User.FullName;
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SalesPersonId = item.Id;
                        this.ShopProperty.ShopSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(e => e.Id == this.SalesPersonId).FirstOrDefault();
                        this.FindControl("ShopSalesPerson").IsReadOnly = true;
                        break;
                    }
                }
                //for some reason LINQ does not work with external datasource link, use iteration
                foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.CustomerId = item.Id;
                        this.FindControl("ShopCustomer").IsReadOnly = true;
                        break;
                    }
                }
            }
        }

        partial void CreateNewShop_Saved()
        {
            // Write your code here.
            this.Close(false);
            Application.Current.ShowDefaultScreen(this.ShopProperty);
        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }
    }
}